package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.RepairContractBadAmountDTO;
import com.xbongbong.pro.script.pojo.dto.RepairContractDataDTO;
import com.xbongbong.pro.script.pojo.dto.RepairContractPerformanceDataDTO;
import com.xbongbong.pro.script.pojo.dto.RepairDepartmentIdBecauseApiDTO;
import com.xbongbong.pro.script.pojo.dto.UpdateFundManagemenExpalinstDTO;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.service.RepairExplainService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2019-11-25 14:33
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX+"/script/contract")
public class ContractScriptController {
	@Resource
	private ProBaseConfig proBaseConfig;
	@Resource
	private RepairExplainService repairExplainService;
	@RequestMapping(value = "/explains", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String explains(@RequestBody @Valid UpdateFundManagemenExpalinstDTO updateFundManagemenExpalinstDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(updateFundManagemenExpalinstDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO detailTabScriptVO = repairExplainService.updateContractExplains(updateFundManagemenExpalinstDTO);
				response = new XbbResponse<>(detailTabScriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairData(@RequestBody @Valid RepairContractDataDTO repairContractDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO detailTabScriptVO = repairExplainService.scriptContractData(repairContractDataDTO);
				response = new XbbResponse<>(detailTabScriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairSerialNoData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairSerialData(@RequestBody @Valid RepairContractDataDTO repairContractDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO detailTabScriptVO = repairExplainService.scriptContractSerialNo(repairContractDataDTO);
				response = new XbbResponse<>(detailTabScriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairContractBad", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairSerialData(@RequestBody @Valid RepairContractBadAmountDTO repairContractBadAmountDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractBadAmountDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO scriptContractBadAmount = repairExplainService.scriptContractBadAmount(repairContractBadAmountDTO);
				response = new XbbResponse<>(scriptContractBadAmount);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairDepartmentIdBecauseApi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairDepartmentIdBecauseApi(@RequestBody @Valid RepairDepartmentIdBecauseApiDTO repairDepartmentIdBecauseApiDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairDepartmentIdBecauseApiDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO scriptVO = repairExplainService.repairDepartmentId(repairDepartmentIdBecauseApiDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/scriptContractLinkBusinessSingleData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String scriptContractLinkBusinessSingleData(@RequestBody @Valid RepairContractDataDTO repairContractDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO scriptVO = repairExplainService.scriptContractLinkBusinessSingleData(repairContractDataDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
	
	@RequestMapping(value = "/scriptContractProcessData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String scriptContractProcessData(@RequestBody @Valid RepairContractDataDTO repairContractDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO scriptVO = repairExplainService.scriptContractProcessData(repairContractDataDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairPerformanceData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairPerformanceData(@RequestBody @Valid RepairContractPerformanceDataDTO repairContractPerformanceDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractPerformanceDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO detailTabScriptVO = repairExplainService.repairPerformanceData(repairContractPerformanceDataDTO);
				response = new XbbResponse<>(detailTabScriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}

	@RequestMapping(value = "/repairContractData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String repairContractData(@RequestBody @Valid RepairContractPerformanceDataDTO repairContractPerformanceDataDTO, BindingResult br) throws XbbException {
		XbbResponse<ScriptVO> response;
		if ( br.hasErrors() ) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				if ( !proBaseConfig.getXbbCode().equals(repairContractPerformanceDataDTO.getXbbCode()) ) {
					throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
				}
				ScriptVO detailTabScriptVO = repairExplainService.repairContractData(repairContractPerformanceDataDTO);
				response = new XbbResponse<>(detailTabScriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
}
